package org.jeecg.modules.simulator.factory.station.db;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.parameter.api.entity.SimExit;
import org.jeecg.modules.parameter.api.entity.SimHall;
import org.jeecg.modules.parameter.api.entity.SimPlatform;
import org.jeecg.modules.parameter.api.entity.SimStation;
import org.jeecg.modules.parameter.biz.mapper.SimStationExitMapper;
import org.jeecg.modules.parameter.biz.mapper.SimStationHallMapper;
import org.jeecg.modules.parameter.biz.mapper.SimStationMapper;
import org.jeecg.modules.parameter.biz.mapper.SimStationPlatformMapper;
import org.jeecg.modules.simulator.factory.BasicStationLoader;
import org.jeecg.modules.simulator.model.line.station.equipment.StationConcourser;
import org.jeecg.modules.simulator.model.line.station.equipment.StationEntryExitGate;
import org.jeecg.modules.simulator.model.line.station.equipment.StationPlatform;
import org.jeecg.modules.simulator.model.line.station.station.SimpleStation;
import org.jeecg.modules.simulator.model.line.station.station.Station;
import org.jeecg.modules.simulator.model.line.station.station.StationState;
import org.jeecg.modules.simulator.model.line.station.station.TransferStation;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: simulator-b20240918
 * @description:
 * @author: renzhg
 * @create: 2024-10-14 14:53
 **/
@Slf4j
@Primary
@Service
public class SimStationService implements BasicStationLoader {



    /**
     * @description: 按线路分类
     *
     * @author:  renzhg
     * @date:  2024/8/30 11:03
     **/
    @Getter
    private Map<String,List<Station>> stationSimpleMap=new HashMap<>();

    /**
     * @description: 换乘站按站分类
     *
     * @author:  renzhg
     * @date:  2024/8/30 11:04
     **/
    @Getter
    private Map<String, TransferStation> stationTransferMap=new HashMap<>();


//    @Getter
//    private List<Stationbs> stationbsList =new ArrayList<>();
    @Resource
    private SimStationMapper mapper;



    @Resource
    private SimStationExitMapper simStationExitMapper;

    @Resource
    private SimStationPlatformMapper simStationPlatformMapper;


    @Resource
    private SimStationHallMapper simStationHallMapper;

    public List<SimStation> getAllStation() {
        return mapper.getAllStation();
    }
    @Override
    public void loadData(){
        List<SimStation> simStationList= getAllStation();
        Lsaware2station(simStationList);
//        List<SimExit> simExitList = simStationExitMapper.selectList(null);
//        List<SimPlatform> simPlatformList = simStationPlatformMapper.selectList(null);
//        List<SimHall> simHallList = simStationHallMapper.selectList(null);
//        aware2Station(simStationList,simExitList,simPlatformList,simHallList);


    }

    private void Lsaware2station(List<SimStation> simStationList){
        List<Station> stationList=new ArrayList<>();
        List<Station> transferStations=new ArrayList<>();
        for (SimStation simStation : simStationList) {
            Station station = new SimpleStation(simStation.getLineId(), simStation.getStationName());
            station.setId(simStation.getStationId());
            station.setOperatingStartTime(simStation.getOpenTime());
            station.setOperatingCloseTime(simStation.getEndTime());

//            System.out.println(simStation.getStationName());




//            station.setEndPt(Long.parseLong(simStation.getMapOffsetY()));
            setEEls(simStation,station);
            setStationConcouserls(station);
            setPlatFormLS(station);
            if(simStation.getTransferFlag()==null){
                log.error("换乘标志为空");
                continue;
            }
            if(!simStation.getTransferFlag().equals("1")){

                station.setOpen(true);
                station.setTransferStation(false);

            }else{

                station.setOpen(true);
                station.setTransferStation(true);
                transferStations.add(station);
            }

            if(simStation.getMapOffsetX()==null){
                log.error("地图坐标X为空");
                continue;
            }
            if(simStation.getMapOffsetY()==null){
                log.error("地图坐标Y为空");
                continue;
            }
            if(simStation.getMapOffsetY().contains(".")) {
                long oy = (long) Double.parseDouble(simStation.getMapOffsetY());
                station.setEndPt(oy);
            }else{
                station.setEndPt(Long.parseLong(simStation.getMapOffsetY()));
            }

            if(simStation.getMapOffsetX().contains(".")) {
                long ox = (long) Double.parseDouble(simStation.getMapOffsetX());
//                int ox = Integer.parseInt(simStation.getMapOffsetX());
                station.setStartPt(ox);
            }else{
                station.setStartPt(Long.parseLong(simStation.getMapOffsetX()));
            }
            stationList.add(station);

        }

        stationSimpleMap = stationList.stream()
                .collect(Collectors.groupingBy(Station::getLine));

        Map<String, List<Station>> transmap = transferStations.stream()
                .collect(Collectors.groupingBy(Station::getName));

        for (Map.Entry<String, List<Station>> entry : transmap.entrySet()) {
            TransferStation transferStation = new TransferStation(entry.getKey());

            entry.getValue().forEach(station -> {

                transferStation.getTransferStationMap().put(station.getLine(), station);
                station.setTransferStation(true);
                transferStation.setOperatingStartTime(station.getOperatingStartTime());
                transferStation.setOperatingCloseTime(station.getOperatingCloseTime());

            });

            stationTransferMap.put(transferStation.getName(), transferStation);

        }


    }


//临时没有数据生成的方法
    private void setPlatFormLS(Station station){
        StationPlatform stationPlatform = new StationPlatform();
        stationPlatform.setPlatformName("1");
        stationPlatform.setArea(100);
        stationPlatform.setCrossTime(100);
        stationPlatform.setDownLimitSpeed(100);
        stationPlatform.setUpLimitSpeed(100);
        stationPlatform.setDownstate(StationState.running);
        stationPlatform.setOpen(true);
        station.setStationPlatform(stationPlatform);
    }
    private void setPlatform(SimStation simStation,Station station,List<SimPlatform> simPlatformList){
        for(SimPlatform simPlatform:simPlatformList){
            if(simPlatform.getMainStationId().equals(simStation.getStationId())){
//                Platform platform = new Platform()
                  StationPlatform stationPlatform = new StationPlatform();    ;
                stationPlatform.setPlatformName(simPlatform.getPlatformName());

                stationPlatform.setArea((int) Math.ceil(simPlatform.getArea()));
                stationPlatform.setCrossTime(Integer.parseInt(simPlatform.getCrossTime()));
                stationPlatform.setDownLimitSpeed((int) Math.ceil(simPlatform.getDownMaxSpeed()));
                stationPlatform.setUpLimitSpeed((int) Math.ceil(simPlatform.getUpMaxSpeed()));
                if(simPlatform.getOperatingStatus().equals("01")){
                    stationPlatform.setDownstate(StationState.running);
                }else if(simPlatform.getOperatingStatus().equals("02")) {
                    stationPlatform.setDownstate(StationState.stop);
                }else if(simPlatform.getOperatingStatus().equals("03")) {
                    stationPlatform.setDownstate(StationState.limiting);
                }
                stationPlatform.setOpen(true);
                station.setStationPlatform(stationPlatform);
            }
        }

    }


    /**
     * @description: 临时出入口生成数据，因为数据库里面没数据
     *
     * @author:  renzhg
     * @date:  2024/10/16 11:09
     **/
    private void setEEls(SimStation simStation, Station station){
        for(int i=0;i<3;i++){
            StationEntryExitGate stationEntryExitGate = new StationEntryExitGate();
            stationEntryExitGate.setEegName("出口"+i);
            stationEntryExitGate.setBelongStationId(simStation.getStationId());
            stationEntryExitGate.setEntrySpeed(100);
            stationEntryExitGate.setExitSpeed(100);
            station.getEntryExitGateMap().put(stationEntryExitGate.getEegName(),stationEntryExitGate);
            stationEntryExitGate.setStationState(StationState.running);

            stationEntryExitGate.setLimitSpeed(123);

            station.getEntryExitGateMap().put(stationEntryExitGate.getEegName(),stationEntryExitGate);
        }

    }
    private void setSEE(SimStation simStation, Station station,List<SimExit> simExitList) {
        for(SimExit simExit:simExitList){
            if(simExit.getMainStationId().equals(simStation.getStationId())){
                StationEntryExitGate stationEntryExitGate = new StationEntryExitGate();
                stationEntryExitGate.setEegName(simExit.getExitName());
                stationEntryExitGate.setBelongStationId(simStation.getStationId());
                stationEntryExitGate.setEntrySpeed((int) Math.ceil(simExit.getInSpeed()));
                stationEntryExitGate.setExitSpeed((int) Math.ceil(simExit.getOutSpeed()));
                if(simExit.getOperatingStatus().equals("01")) {
                    stationEntryExitGate.setStationState(StationState.running);
                }else if(simExit.getOperatingStatus().equals("02")){
                    stationEntryExitGate.setStationState(StationState.stop);
                } else if(simExit.getOperatingStatus().equals("03")){
                    stationEntryExitGate.setStationState(StationState.limiting);
                }

                stationEntryExitGate.setLimitSpeed((int) Math.ceil(simExit.getMaxSpeed()));

                station.getEntryExitGateMap().put(stationEntryExitGate.getEegName(),stationEntryExitGate);
            }

        }

    }


    private void setStationConcourser(SimStation simStation, Station station, List<SimHall> simHallList) {
//        Hall hall = stationbs.getHall();

        for(SimHall simHall:simHallList){
            if(simHall.getMainStationId().equals(simStation.getStationId())){
                StationConcourser stationConcourse = new StationConcourser();
                stationConcourse.setArea((int) Math.ceil(simHall.getArea()));
                stationConcourse.setCrossTime(Integer.parseInt(simHall.getCrossTime()));
                stationConcourse.setLimitSpeed((int) Math.ceil(simHall.getMaxSpeed()));
                stationConcourse.setOpen(true);
                station.setStationConcourser(stationConcourse);
            }
        }
//        StationConcourser stationConcourse = new StationConcourser();
//        stationConcourse.setArea(hall.getArea());
//        stationConcourse.setCrossTime(hall.getCrossTime());
//        stationConcourse.setLimitSpeed(hall.getLimitSpeed());
//        stationConcourse.setOpen(hall.isOpen());
//        station.setStationConcourser(stationConcourse);
    }

    private void setStationConcouserls(Station station){
        StationConcourser stationConcourse = new StationConcourser();
        stationConcourse.setArea(100);
        stationConcourse.setCrossTime(100);
        stationConcourse.setLimitSpeed(100);
        stationConcourse.setOpen(true);
        station.setStationConcourser(stationConcourse);
    }


    private void aware2Station(List<SimStation> simStationList,
                               List<SimExit> simExitList ,
                               List<SimPlatform> simPlatformList,
                               List<SimHall> simHallList){
        List<Station> stationList=new ArrayList<>();
        List<Station> transferStations=new ArrayList<>();
        for (SimStation simStation : simStationList) {

            Station station = new SimpleStation(simStation.getLineId(), simStation.getStationName());
            station.setId(simStation.getStationId());
            station.setStartPt(Long.parseLong(simStation.getMapOffsetX()));
            station.setEndPt(Long.parseLong(simStation.getMapOffsetY()));
            setSEE(simStation, station,simExitList);
            setPlatform(simStation,station,simPlatformList);
            setStationConcourser(simStation,station,simHallList);
            if(!simStation.getTransferFlag().equals("0")){

                station.setOpen(true);
                station.setTransferStation(false);
                stationList.add(station);

            }else{

                station.setOpen(true);
                station.setTransferStation(true);
                transferStations.add(station);
            }

        }

        stationSimpleMap = stationList.stream()
                .collect(Collectors.groupingBy(Station::getLine));

        Map<String, List<Station>> transmap = transferStations.stream()
                .collect(Collectors.groupingBy(Station::getName));

        for (Map.Entry<String, List<Station>> entry : transmap.entrySet()) {
            TransferStation transferStation = new TransferStation(entry.getKey());

            entry.getValue().forEach(station -> {

                transferStation.getTransferStationMap().put(station.getLine(), station);
                station.setTransferStation(true);
            });
            stationTransferMap.put(transferStation.getName(), transferStation);

        }


    }


}
